﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using YiSha.Util;
using YiSha.Util.Model;
using YiSha.Entity;
using YiSha.Model;
using YiSha.Admin.Web.Controllers;
using YiSha.Entity.YiDongYuan;
using YiSha.Business.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Service.YiDongYuan;
using YiSha.Web.Code;

namespace YiSha.Admin.Web.Areas.YiDongYuan.Controllers
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-09 08:20
    /// 描 述：检测任务控制器类
    /// </summary>
    [Area("YiDongYuan")]
    public class DetectionTaskController : BaseController
    {
        private DetectionTaskBLL detectionTaskBLL = new DetectionTaskBLL();
        private DetectionMechanicsBLL detectionMechanicsBLL = new DetectionMechanicsBLL();
        private DetectionInfoBLL detectionInfoBLL = new DetectionInfoBLL();
        private DetectionResultBLL detectionResultBLL = new DetectionResultBLL();

        #region 视图功能

        [AuthorizeFilter("yidongyuan:detectiontask:view")]
        public ActionResult DetectionTaskIndex(int detectionStatus = -1)
        {
            ViewBag.DetectionStatus = detectionStatus;
            return View();
        }

        //复检
        public ActionResult ShowRecheckForm()
        {
            return View();
        }

        public ActionResult ShowInfoForm()
        {
            return View();
        }

        //检测过程数据
        public ActionResult ShowProcessData()
        {
            return View();
        }

        [AuthorizeFilter("yidongyuan:detectiontask:audit")]
        public ActionResult ShowAuditTask()
        {
            return View();
        }

        public ActionResult CMAReport([FromQuery] long id = 0, [FromQuery] string code = "")
        {//根据taskId查询DetectionTask
            DetectionTaskEntity task;
            if (id > 0)
            {
                task = detectionTaskBLL.GetEntity(id).Result.Data;
            }
            else
            {
                task = detectionTaskBLL.GetLastEntityByCode(code).Result.Data;
            }
            ViewBag.Task = task;

            //根据MechanicsId查询机械信息--DetectionMechanics
            DetectionMechanicsEntity mechanics = detectionMechanicsBLL.GetEntity(task.MechanicsId.Value).Result.Data;
            ViewBag.Mechanics = mechanics;

            //根据TaskId查询DetectionInfo
            DetectionInfoEntity info = detectionInfoBLL.GetEntity(task.DetectionInfoId.Value).Result.Data;
            ViewBag.Info = info;

            //根据ResultDataId查询DetectionResult
            DetectionResultEntity result = detectionResultBLL.GetEntity(info.ResultDataId.Value).Result.Data;
            ViewBag.Result = result;
            return View();
        }

        public ActionResult DeviceCert(long id)
        {//根据taskId查询DetectionTask
            DetectionTaskEntity task = detectionTaskBLL.GetEntity(id).Result.Data;

            //根据MechanicsId查询机械信息--DetectionMechanics
            DetectionMechanicsEntity mechanics = detectionMechanicsBLL.GetEntity(task.MechanicsId.Value).Result.Data;
            ViewBag.Mechanics = mechanics;

            //根据TaskId查询DetectionInfo
            DetectionInfoEntity info = detectionInfoBLL.GetEntity(task.DetectionInfoId.Value).Result.Data;

            //根据ResultDataId查询DetectionResult
            DetectionResultEntity result = detectionResultBLL.GetEntity(info.ResultDataId.Value).Result.Data;
            ViewBag.Result = result;
            return View();
        }

        #endregion 视图功能

        #region 获取数据

        [HttpGet]
        [AuthorizeFilter("yidongyuan:detectiontask:search")]
        public async Task<ActionResult> GetListJson(DetectionTaskListParam param)
        {
            TData<List<DetectionTaskEntity>> obj = await detectionTaskBLL.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("yidongyuan:detectiontask:search")]
        public async Task<ActionResult> GetPageListJson(DetectionTaskListParam param, Pagination pagination)
        {
            TData<List<DetectionTaskEntity>> obj = await detectionTaskBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> GetFormJson(long id)
        {
            TData<DetectionTaskEntity> obj = await detectionTaskBLL.GetEntity(id);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> DetectionProcessData(long id)
        {
            TData<List<ProcessDataEntity>> obj = await detectionTaskBLL.GetListByInfoId(id);
            return Json(obj);
        }

        #endregion 获取数据

        #region 提交数据

        [HttpPost]
        [AuthorizeFilter("yidongyuan:detectiontask:add,yidongyuan:detectiontask:edit")]
        public async Task<ActionResult> SaveFormJson(DetectionTaskEntity entity)
        {
            TData<string> obj = await detectionTaskBLL.SaveForm(entity);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("yidongyuan:detectiontask:add,yidongyuan:detectiontask:edit")]
        public async Task<ActionResult> RecheckFormJson(RecheckEntity entity)
        {
            OperatorInfo user = await Operator.Instance.Current("");
            entity.AuditUserId = user.UserId;
            TData<string> obj = await detectionTaskBLL.SaveRecheckForm(entity);
            return Json(obj);
        }

        //更新审核结果
        [HttpPost]
        [AuthorizeFilter("yidongyuan:detectiontask:audit")]
        public async Task<ActionResult> UpdateTaskAudit(string id, int auditStatus, string auditReason, long? auditUserId)
        {
            OperatorInfo user = await Operator.Instance.Current("");

            TData<string> obj = await detectionTaskBLL.UpdateTaskAudit(id, auditStatus, auditReason, user.UserId);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("yidongyuan:detectiontask:delete")]
        public async Task<ActionResult> DeleteFormJson(string ids)
        {
            TData obj = await detectionTaskBLL.DeleteForm(ids);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("yidongyuan:detectiontask:audit")]
        public ActionResult DoUpload(long id)
        {
            TData obj = detectionTaskBLL.Upload2Hzx(id);
            return Json(obj);
        }

        #endregion 提交数据
    }
}
